home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / realpower.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-09  |  3.0 KB  |  102 lines

  1. {
  2. GPC demo program for the GMP unit.
  3. Computing arbitrary real powers and roots.
  4.  
  5. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  6.  
  7. Author: Frank Heckenbach <frank@pascal.gnu.de>
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation, version 2.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; see the file COPYING. If not, write to
  20. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22.  
  23. As a special exception, if you incorporate even large parts of the
  24. code of this demo program into another program with substantially
  25. different functionality, this does not cause the other program to
  26. be covered by the GNU General Public License. This exception does
  27. not however invalidate any other reasons why it might be covered
  28. by the GNU General Public License.
  29. }
  30.  
  31. program RealPower;
  32.  
  33. uses GPC, GMP;
  34.  
  35. const
  36.   DefaultNumberOfDigits = 100;
  37.  
  38. procedure mpf_ReadStr (const s : String; var Dest : mpf_t);
  39. begin
  40.   mpf_init (Dest);
  41.   if mpf_set_str (Dest, s, 10) <> 0 then
  42.     begin
  43.       Writeln ('Invalid numeric format.');
  44.       RunError
  45.     end
  46. end;
  47.  
  48. var
  49.   d : MedCard;
  50.   p_r, a_r, e_r : mpf_t;
  51.   BaseStr, ExpStr, DigitsStr, TempStr : TString;
  52.   RootFlag : Boolean;
  53.   ex : mp_exp_t;
  54.   s, q : CString;
  55.  
  56. begin
  57.   case ParamCount of
  58.     0    : begin
  59.              Writeln ('Real power/root computation.');
  60.              Write ('Enter the base: ');
  61.              Readln (BaseStr);
  62.              Write ('Enter the exponent (or `1/n'' for a root): ');
  63.              Readln (ExpStr);
  64.              Write ('Enter the number of digits wanted (default: ', DefaultNumberOfDigits, '): ');
  65.              Readln (DigitsStr);
  66.            end;
  67.     2, 3 : begin
  68.              BaseStr := ParamStr (1);
  69.              ExpStr := ParamStr (2);
  70.              DigitsStr := ParamStr (3)
  71.            end;
  72.     else
  73.       Writeln (StdErr, 'Real power/root computation.');
  74.       Writeln (StdErr, 'Usage: ', ParamStr (0), ' [base [1/]exponent [number_of_digits]]');
  75.       Halt (1)
  76.   end;
  77.   if DigitsStr = '' then d := DefaultNumberOfDigits else ReadStr (DigitsStr, d);
  78.   mpf_set_default_prec (Round (d * Ln (10) / Ln (2)) + 64);
  79.   mpf_ReadStr (BaseStr, a_r);
  80.   RootFlag := Copy (ExpStr, 1, 2) = '1/';
  81.   TempStr := ExpStr;
  82.   if RootFlag then Delete (TempStr, 1, 2);
  83.   mpf_ReadStr (TempStr, e_r);
  84.   if RootFlag then mpf_ui_div (e_r, 1, e_r);
  85.   mpf_init (p_r);
  86.   mpf_pow (p_r, a_r, e_r);
  87.   Write (BaseStr, ' ^ ', ExpStr, ' ~ ');
  88.   s := mpf_get_str (nil, ex, 10, d, p_r);
  89.   {$local X+}
  90.   if s [0] = '-' then
  91.     begin
  92.       Write ('-');
  93.       q := s + 1
  94.     end
  95.   else
  96.     q := s;
  97.   Writeln ('0.', q, 'e', ex);
  98.   {$endlocal}
  99.   Dispose (s);
  100.   mpf_clear (p_r)
  101. end.
  102.